package gui.client;

import java.awt.Color;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.util.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.joda.time.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import system.FieldValidator;
import system.SystemMode;
import system.SystemTypes;
import system.VCP;
import vcpClasses.Order;
import vcpClasses.Rates;
import vcpClasses.SubscribedClient;

import com.toedter.calendar.JDateChooser;

import control.LoginController;
import control.OrderController;
import control.SubscriptionController;

import javax.swing.JTextField;
import javax.swing.DefaultComboBoxModel;
/**
 * this class use to Subscription order Screen
 * @author G8
 *
 */
@SuppressWarnings("serial")
public class SubscriptionScreen extends JPanel implements ActionListener
{
	public JButton btnLogout;
	public JButton btnBack;
	public JButton btnSave;
	private JTextField IDNumber;
	private JTextField VehiclesNumber;
	private JLabel lblIDNumber;
	private JLabel lblVehiclesNumber;
	private JLabel lblCheckOutTime;
	private JLabel lblStartDate;
	private JComboBox<Integer> cbParkingLot;
	private JLabel lblSelectParking;
	private JDateChooser startSubscibe;
	private JFormattedTextField textFieldCheckOut;
	private JComboBox<String> comboBoxType;
	private SubscriptionController subscriptionController;
	/**
	 *Subscription order Screen Constructor 
	 */
	public  SubscriptionScreen()
	{
		super();
		initialize();
	}
	/**
	 * This method contains the view of the Panel
	 */
	private void initialize()
	{
		subscriptionController = new SubscriptionController(null);
		
		setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Subscription Screen", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)));
		setBackground(SystemColor.menu);
		setBounds(VCP.guiX,VCP.guiY,500,400);
		setLayout(null);
		VCP.frame.setBounds(this.getBounds());
		
		btnBack = new JButton("Back");
		btnBack.setIcon(new ImageIcon(AdvanceOrderScreen.class.getResource("/img/back.png")));
		btnBack.setSize(150, 30);
		btnBack.setBounds(281, 22, 102, 35);
		btnBack.addActionListener(this);
		add(btnBack);
		
		btnLogout = new JButton("Logout");
		btnLogout.setSize(150, 30);
		btnLogout.setBounds(393, 22, 78, 35);
		btnLogout.addActionListener(this);
		add(btnLogout);
		
		btnSave = new JButton("Subscribe");
		btnSave.setBounds(201, 288, 97, 35);
		btnSave.addActionListener(this);
		add(btnSave);
		
		IDNumber = new JTextField();
		IDNumber.setBounds(268, 123, 87, 20);
		add(IDNumber);
		
		VehiclesNumber = new JTextField();
		VehiclesNumber.setBounds(268, 153, 87, 20);
		add(VehiclesNumber);
		
		lblIDNumber = new JLabel("ID Number :");
		lblIDNumber.setBounds(123, 123, 160, 20);
		add(lblIDNumber);
		
		lblVehiclesNumber = new JLabel("Vehicle Number :");
		lblVehiclesNumber.setBounds(123, 153, 160, 20);
		add(lblVehiclesNumber);
		
		lblStartDate = new JLabel("Start Date :");
		lblStartDate.setBounds(123, 183, 160, 20);
		add(lblStartDate);
		
		cbParkingLot = new JComboBox <Integer>();
		for(int t=1;t<=6;t++)
			cbParkingLot.addItem(t);
		cbParkingLot.setBounds(268, 214, 87, 20);
		add(cbParkingLot);
		
		lblSelectParking = new JLabel("Parking Lot :");
		lblSelectParking.setBounds(123, 213, 160, 20);
		add(lblSelectParking);
		
		try {
			MaskFormatter formatTimeOut = new MaskFormatter("##:##");
			formatTimeOut.setValidCharacters("0123456789");
		} catch (ParseException e) {
			
			e.printStackTrace();
		}
		
		lblCheckOutTime = new JLabel("Check Out Time :");
		lblCheckOutTime.setBounds(123, 245, 160, 20);
		add(lblCheckOutTime);
		
		startSubscibe = new JDateChooser();
		startSubscibe.setDateFormatString("yyyy-MM-dd");
		startSubscibe.setBounds(268, 183, 87, 20);
		add(startSubscibe);
		
		try 
		{
			MaskFormatter formatCheckInOut = new MaskFormatter("##:##");
			formatCheckInOut.setValidCharacters("0123456789");
		textFieldCheckOut = new JFormattedTextField(formatCheckInOut);
		}catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		textFieldCheckOut.setBounds(268, 245, 87, 20);
		add(textFieldCheckOut);
		textFieldCheckOut.setColumns(10);
		
		JLabel lblSubType = new JLabel("Subscription Type :");
		lblSubType.setBounds(123, 92, 160, 20);
		add(lblSubType);
		
		comboBoxType = new JComboBox<String>();
		comboBoxType.setModel(new DefaultComboBoxModel(new String[] {"Normal", "Full"}));
		comboBoxType.setToolTipText("");
		comboBoxType.setBounds(268, 92, 87, 20);
		comboBoxType.addActionListener(this);
		add(comboBoxType);

	}

	/**
	 * This method listens to the buttons on the panel
	 * @param e is the buttons the user Click
	 */
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == comboBoxType)
		{
			if (comboBoxType.getSelectedItem().equals("Full")){
				cbParkingLot.setEnabled(false);
				textFieldCheckOut.setText("00:00");
				textFieldCheckOut.setEnabled(false);
			}
			else{
				cbParkingLot.setEnabled(true);
				textFieldCheckOut.setEnabled(true);
			}
		}
		if (e.getSource() == btnSave)
		{
			IDNumber.setBackground(Color.WHITE);
			VehiclesNumber.setBackground(Color.WHITE);
			textFieldCheckOut.setBackground(Color.WHITE);
			startSubscibe.setBackground(Color.WHITE);
			
			SimpleDateFormat thedate = new SimpleDateFormat("yyyy-MM-dd");
			String date1 = thedate.format(startSubscibe.getDate());
			
			if (FieldValidator.checkNumeric(IDNumber.getText()) && FieldValidator.checkNumeric(VehiclesNumber.getText())
					&& FieldValidator.checkTime(textFieldCheckOut.getText()) && FieldValidator.checkDate(startSubscibe.getDate()))
			{
				SubscribedClient subscribedClient = new SubscribedClient();
				subscribedClient.setStartDate(date1);
				if (comboBoxType.getSelectedItem().equals("Full")){
					subscribedClient.setTypeOfSubscription("FULL");
					subscribedClient.setClientLeaveTime("00:00");
				}	
				else{
					subscribedClient.setTypeOfSubscription("NORMAL");
					subscribedClient.setClientLeaveTime(textFieldCheckOut.getText());
				}
				subscribedClient.setLotNumber((int) cbParkingLot.getSelectedItem());
				subscribedClient.setClientNumber(Integer.parseInt(IDNumber.getText()));
				subscribedClient.setVehicleNumber(Integer.parseInt(VehiclesNumber.getText()));
				SystemTypes clientToSubscribe = new SystemTypes(subscribedClient,null);
				SystemTypes subResult = subscriptionController.createNewSubscription(clientToSubscribe);
				if (subResult.getSysMode().equals(SystemMode.OK))
					JOptionPane.showMessageDialog(null,subResult.getObj().toString(), "Success",JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showMessageDialog(null,subResult.getObj().toString(), "Error",JOptionPane.ERROR_MESSAGE);
			}
			else{
				if (!FieldValidator.checkNumeric(IDNumber.getText())) IDNumber.setBackground(Color.RED);
				if (!FieldValidator.checkNumeric(VehiclesNumber.getText())) VehiclesNumber.setBackground(Color.RED);
				if (!FieldValidator.checkTime(textFieldCheckOut.getText())) textFieldCheckOut.setBackground(Color.RED);
				if (!FieldValidator.checkDate(startSubscibe.getDate())) startSubscibe.setBackground(Color.RED);
				JOptionPane.showMessageDialog(null,"Invalid field(s) detected !", "Error",JOptionPane.ERROR_MESSAGE);
			}
		}
		
		if (e.getSource() == btnBack)
		{
			VCP.setScreen(this, VCP.prevScreen);
			VCP.prevScreen.setBounds(VCP.guiX,VCP.guiY,500,400);
			VCP.prevScreen.setLayout(null);
			VCP.frame.setBounds(VCP.prevScreen.getBounds());
		}
		if (e.getSource() == btnLogout)
		{
			SystemTypes userToLogout = new SystemTypes(VCP.currentUser,null);
			boolean logoutRes = LoginController.LogOut(userToLogout);
			if (logoutRes)
				VCP.setGUI(0, this);
			else{
				JOptionPane.showMessageDialog(null, "Failed to logout!", "Error",
	                    JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
